<?php
	require_once ("db_connection.php");
	
	$id = "AF-" . uniqid();
	$tempcityid = "";
	$contacts = '';
	
	
	for($i=0;$i<count($_POST["contact"]);$i++)
	{	
		$contacts = $contacts . $_POST["contact"][$i] . ',';
	}
	
	$result = mysql_query("SELECT * FROM city WHERE cityname = '$_POST[cityname]'", $con);	
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
		$tempcityid = $row['cityid'] ;
	
	/*$res = mysql_query("select count(*) from affiliate where cityid = '$tempcityid'", $con);
	$row1 = mysql_fetch_row($res);
	$total = $row1[0];
	if ($row1[0] > 0)
	{
		//echo "exist";
		$priorityno =  $total+1;
	}
	else
	{
		//echo "not exist";
		$priorityno = 1;
	}*/

	$query = "INSERT INTO affiliate(affiliateid, companyname, companyaddress,companyemail, landline, faxno,mobileno1, cityid ,websiteurl,contactname, contactemail, priority)
		VALUES('$id', '$_POST[companyname]', '$_POST[companyaddress]', '$_POST[companyemail]', '$_POST[landline]' ,'$_POST[faxno]', '$_POST[mobileno1]', '$tempcityid', '$_POST[websiteurl]','$_POST[contactname]', '$contacts', '$_POST[priorityno]'  )";
	
	if(!mysql_query($query, $con)) {
		die('Error: '. mysql_error());
	}
	
	$data = array("Saved!", $id);
	
	mysql_close($con);
	
	echo json_encode($data);
	flush();
?>